# p站
import urllib.request
import json
import jsonpath
import os
import re

# https://img2.huashi6.com/images/resource/thumbnail/2022/08/02/154327_20756220838.jpg?imageMogr2/quality/75/interlace/1/thumbnail/223x/gravity/North/crop/223x313/format/webp
print("文件将保存在  ’pixiv文件夹‘  中")
print("正在下载，请稍后......")
if not os.path.exists('./pixiv文件夹'):
    os.mkdir('./pixiv文件夹')

headers = {
    "User-Agent": "Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/101.0.0.0Safari/537.36",
}
# 151页
for page in range(1, 152):
    url = f'https://rt.huashi6.com/front/index/load_pc_data?_ts_=1659441701694&cursor={page}-1659440992772'

    # 请求对象的定制
    request = urllib.request.Request(url=url, headers=headers)

    response = urllib.request.urlopen(request)

    html = response.read().decode('utf-8')

    # print(html)
    with open(f'./pixiv-{page}.json', 'w', encoding='utf-8') as f:
        f.write(html)

    # jsonpath 解析JSON文件
    obj = json.load(open(f'pixiv-{page}.json', 'r', encoding='utf-8'))
    img_list = jsonpath.jsonpath(obj, '$..thumbnailPath')
    name_list = jsonpath.jsonpath(obj, '$..title')
    # print(img_list)
    # print(name_list)

    # 循环遍历下载图片
    for i in range(len(img_list)):
        src = 'https://img2.huashi6.com/' + img_list[i]
        name = name_list[i]
        name = re.sub('[\\\/|":*?<>]', '-', name)
        suffix = src.split('.')[-1]
        try:
            urllib.request.urlretrieve(url=src, filename='./pixiv文件夹/' + name + '.' + suffix)
        except:
            print(f'{name}--下载失败!')

        print(f'{name}--下载完成')
    print(f'************当前在{page}页************')

    # 删除当前的JSON文件
    # os.path.join 路径拼接
    # os.path.dirname(__file__) 获取当前文件夹的路径

    os.remove(os.path.join(os.path.dirname(__file__), f'pixiv-{page}.json'))
print("所有图片已经下载完！")
# if __name__ == '__main__':
# 获取网页数据源码
# 解析源码的数据
# 下载资源,持久化存储
